﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Net;
using System.Xml.Linq;

namespace Springbok.Service
{
    public class WMSService : IWMS
    {
        public string GetData(String value)
        {
            return string.Format("You entered: {0}", value);
        }


        public Stream GetMap(string mapname, string layers, string imageformat, string version, string srs, string bbox, string width, string height)
        {
            FileStream fs = File.OpenRead(@"D:\Users\Bluenet\Desktop\topp-states.png");
            if (WebOperationContext.Current != null)
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
                String path =
                    WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString().Substring(36);
                path = "http://localhost:8080/geoserver" + path;

                using (var client = new WebClient())
                {
                    try
                    {
                        File.Delete(@"D:\Users\Bluenet\Desktop\now.jpg");
                    }
                    catch (Exception)
                    {


                    }
                    client.DownloadFile(path, @"D:\Users\Bluenet\Desktop\now.jpg");
                    fs = File.OpenRead(@"D:\Users\Bluenet\Desktop\now.jpg");
                }


            }
            return fs;
        }


        public string GetFeatureInfo(string mapname, string version, string imageformat, string width, string height, string layer, string sldurl)
        {
            return "success";
        }



        public XElement GetFeatureInfo(string mapname, string qlayers, string layers, string infoformat, string imageformat, string featurecount, string version, string srs, string bbox, string width, string height, string x, string y)
        {
            throw new NotImplementedException();
        }
    }
}
